home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / MacPerl 5.1.3 / Mac_Perl_513_src / perl5.002 / lib / Socket.pm < prev    next >
Encoding:
Perl POD Document  |  1997-01-08  |  10.8 KB  |  390 lines  |  [TEXT/MPS ]

  1. package Socket;
  2.  
  3. use vars qw($VERSION @ISA @EXPORT);
  4. $VERSION = "1.5";
  5.  
  6. =head1 NAME
  7.  
  8. Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C socket.h defines and structure manipulators 
  9.  
  10. =head1 SYNOPSIS
  11.  
  12.     use Socket;
  13.  
  14.     $proto = getprotobyname('udp');
  15.     socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
  16.     $iaddr = gethostbyname('hishost.com');
  17.     $port = getservbyname('time', 'udp');
  18.     $sin = sockaddr_in($port, $iaddr);
  19.     send(Socket_Handle, 0, 0, $sin);
  20.  
  21.     $proto = getprotobyname('tcp');
  22.     socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
  23.     $port = getservbyname('smtp');
  24.     $sin = sockaddr_in($port,inet_aton("127.1"));
  25.     $sin = sockaddr_in(7,inet_aton("localhost"));
  26.     $sin = sockaddr_in(7,INADDR_LOOPBACK);
  27.     connect(Socket_Handle,$sin);
  28.  
  29.     ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
  30.     $peer_host = gethostbyaddr($iaddr, AF_INET);
  31.     $peer_addr = inet_ntoa($iaddr);
  32.  
  33.     $proto = getprotobyname('tcp');
  34.     socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
  35.     unlink('/tmp/usock');
  36.     $sun = sockaddr_un('/tmp/usock');
  37.     connect(Socket_Handle,$sun);
  38.  
  39. =head1 DESCRIPTION
  40.  
  41. This module is just a translation of the C F<socket.h> file.
  42. Unlike the old mechanism of requiring a translated F<socket.ph>
  43. file, this uses the B<h2xs> program (see the Perl source distribution)
  44. and your native C compiler.  This means that it has a 
  45. far more likely chance of getting the numbers right.  This includes
  46. all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc.
  47.  
  48. In addition, some structure manipulation functions are available:
  49.  
  50. =item inet_aton HOSTNAME
  51.  
  52. Takes a string giving the name of a host, and translates that
  53. to the 4-byte string (structure). Takes arguments of both
  54. the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
  55. cannot be resolved, returns undef.
  56.  
  57. =item inet_ntoa IP_ADDRESS
  58.  
  59. Takes a four byte ip address (as returned by inet_aton())
  60. and translates it into a string of the form 'd.d.d.d'
  61. where the 'd's are numbers less than 256 (the normal
  62. readable four dotted number notation for internet addresses).
  63.  
  64. =item INADDR_ANY
  65.  
  66. Note: does not return a number, but a packed string.
  67.  
  68. Returns the 4-byte wildcard ip address which specifies any
  69. of the hosts ip addresses. (A particular machine can have
  70. more than one ip address, each address corresponding to
  71. a particular network interface. This wildcard address
  72. allows you to bind to all of them simultaneously.)
  73. Normally equivalent to inet_aton('0.0.0.0').
  74.  
  75. =item INADDR_LOOPBACK
  76.  
  77. Note - does not return a number.
  78.  
  79. Returns the 4-byte loopback address. Normally equivalent
  80. to inet_aton('localhost').
  81.  
  82. =item INADDR_NONE
  83.  
  84. Note - does not return a number.
  85.  
  86. Returns the 4-byte invalid ip address. Normally equivalent
  87. to inet_aton('255.255.255.255').
  88.  
  89. =item sockaddr_in PORT, ADDRESS
  90.  
  91. =item sockaddr_in SOCKADDR_IN
  92.  
  93. In an array context, unpacks its SOCKADDR_IN argument and returns an array
  94. consisting of (PORT, ADDRESS).  In a scalar context, packs its (PORT,
  95. ADDRESS) arguments as a SOCKADDR_IN and returns it.  If this is confusing,
  96. use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
  97.  
  98. =item pack_sockaddr_in PORT, IP_ADDRESS
  99.  
  100. Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
  101. inet_aton()). Returns the sockaddr_in structure with those arguments
  102. packed in with AF_INET filled in.  For internet domain sockets, this
  103. structure is normally what you need for the arguments in bind(),
  104. connect(), and send(), and is also returned by getpeername(),
  105. getsockname() and recv().
  106.  
  107. =item unpack_sockaddr_in SOCKADDR_IN
  108.  
  109. Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
  110. returns an array of two elements: the port and the 4-byte ip-address.
  111. Will croak if the structure does not have AF_INET in the right place.
  112.  
  113. =item sockaddr_un PATHNAME
  114.  
  115. =item sockaddr_un SOCKADDR_UN
  116.  
  117. In an array context, unpacks its SOCKADDR_UN argument and returns an array
  118. consisting of (PATHNAME).  In a scalar context, packs its PATHANE
  119. arguments as a SOCKADDR_UN and returns it.  If this is confusing, use
  120. pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
  121. These are only supported if your system has <sys/un.h>.
  122.  
  123. =item pack_sockaddr_un PATH
  124.  
  125. Takes one argument, a pathname. Returns the sockaddr_un structure with
  126. that path packed in with AF_UNIX filled in. For unix domain sockets, this
  127. structure is normally what you need for the arguments in bind(),
  128. connect(), and send(), and is also returned by getpeername(),
  129. getsockname() and recv().
  130.  
  131. =item unpack_sockaddr_un SOCKADDR_UN
  132.  
  133. Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
  134. and returns the pathname.  Will croak if the structure does not
  135. have AF_UNIX in the right place.
  136.  
  137. =back
  138.  
  139. A number of additional manipulation functions are available for the Apple Macintosh
  140.  
  141. =item sockaddr_atlk NET, NODE, SOCKET
  142.  
  143. =item sockaddr_atlk SOCKADDR_ATLK
  144.  
  145. In an array context, unpacks its SOCKADDR_ATLK argument and returns an array
  146. consisting of (NET,NODE,SOCKET).  In a scalar context, packs its 
  147. arguments as a SOCKADDR_ATLK and returns it.  If this is confusing, use
  148. pack_sockaddr_atlk() and unpack_sockaddr_atlk() explicitly.
  149.  
  150. =item pack_sockaddr_atlk NET, NODE, SOCKET
  151.  
  152. Returns the sockaddr_atlk structure withthe arguments packed in with AF_APPLETALK
  153. filled in. For AppleTalk (ADSP) sockets, this structure is normally what you need 
  154. for the arguments in bind(), connect(), and send(), and is also returned by 
  155. getpeername(), getsockname() and recv().
  156.  
  157. =item unpack_sockaddr_atlk SOCKADDR_ATLK
  158.  
  159. Takes a sockaddr_atlk structure (as returned by pack_sockaddr_atlk())
  160. and returns an array of three elements: the network number, node number, and socket
  161. number.  Will croak if the structure does not have AF_APPLETALK in the right place.
  162.  
  163. =item sockaddr_atlk_sym OBJECT, TYPE, ZONE
  164.  
  165. In an array context, croaks.  In a scalar context, packs its 
  166. arguments as a SOCKADDR_ATLK_SYM and returns it.  If this is confusing, use
  167. pack_sockaddr_atlk_sym() explicitly.
  168.  
  169. =item pack_sockaddr_atlk_sym OBJECT, TYPE, ZONE
  170.  
  171. Returns the sockaddr_atlk_sym structure with the arguments packed in with 
  172. ATALK_SYMADDR filled in. For AppleTalk (ADSP) sockets, you can use this
  173. structure for the arguments in bind() and connect(). No socket calls ever
  174. return a sockaddr_atlk_sym, they always return a numeric sockaddr_atlk
  175. instead.
  176.  
  177. =item sockaddr_ppc LOC_SEL, OBJ, TYPE, ZONE, SCRIPT, NAME, PORTSEL, (TYPE | TYPE, CREATOR) 
  178.  
  179. =item sockaddr_ppc SOCKADDR_PPC
  180.  
  181. In an array context, unpacks its SOCKADDR_PPC argument and returns an array
  182. consisting of (LOC_SEL, OBJ, TYPE, ZONE, SCRIPT, NAME, PORTSEL, (TYPE | TYPE, 
  183. CREATOR)).  In a scalar context, packs its  arguments as a SOCKADDR_PPC and 
  184. returns it.  If this is confusing, use pack_sockaddr_ppc() and 
  185. unpack_sockaddr_ppc() explicitly.
  186.  
  187. =item pack_sockaddr_ppc LOC_SEL, OBJ, TYPE, ZONE, SCRIPT, NAME, PORTSEL, (TYPE | TYPE, CREATOR)
  188.  
  189. Returns the sockaddr_ppc structure withthe arguments packed in with AF_PPC
  190. filled in. For Process-to-Process Communication toolbox sockets, this structure 
  191. is normally what you need  for the arguments in bind(), connect(), and send(), 
  192. and is also returned by getpeername(), getsockname() and recv().
  193.  
  194. =item unpack_sockaddr_ppc SOCKADDR_PPC
  195.  
  196. Takes a sockaddr_ppc structure (as returned by pack_sockaddr_ppc())
  197. and returns an array of 8 or 9 elements: the location variant selector,
  198. location object, type, and zone strings, the port name script code, the port 
  199. name, the port variant selector, and the port type or creator and file
  200. type. Will croak if the structure does not have AF_PPC in the right place.
  201.  
  202. =cut
  203.  
  204. use Carp;
  205.  
  206. require Exporter;
  207. use AutoLoader;
  208. require DynaLoader;
  209. @ISA = qw(Exporter DynaLoader);
  210. @EXPORT = qw(
  211.     inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
  212.     pack_sockaddr_un unpack_sockaddr_un
  213.     sockaddr_in sockaddr_un
  214.     sockaddr_atlk sockaddr_atlk_sym  sockaddr_ppc
  215.     pack_sockaddr_atlk pack_sockaddr_atlk_sym pack_sockaddr_ppc
  216.     unpack_sockaddr_atlk unpack_sockaddr_ppc
  217.     INADDR_ANY INADDR_LOOPBACK INADDR_NONE
  218.     AF_802
  219.     AF_APPLETALK
  220.     AF_CCITT
  221.     AF_CHAOS
  222.     AF_DATAKIT
  223.     AF_DECnet
  224.     AF_DLI
  225.     AF_ECMA
  226.     AF_GOSIP
  227.     AF_HYLINK
  228.     AF_IMPLINK
  229.     AF_INET
  230.     AF_LAT
  231.     AF_MAX
  232.     AF_NBS
  233.     AF_NIT
  234.     AF_NS
  235.     AF_OSI
  236.     AF_OSINET
  237.     AF_PUP
  238.     AF_SNA
  239.     AF_UNIX
  240.     AF_UNSPEC
  241.     AF_X25
  242.     MSG_DONTROUTE
  243.     MSG_MAXIOVLEN
  244.     MSG_OOB
  245.     MSG_PEEK
  246.     PF_802
  247.     PF_APPLETALK
  248.     PF_CCITT
  249.     PF_CHAOS
  250.     PF_DATAKIT
  251.     PF_DECnet
  252.     PF_DLI
  253.     PF_ECMA
  254.     PF_GOSIP
  255.     PF_HYLINK
  256.     PF_IMPLINK
  257.     PF_INET
  258.     PF_LAT
  259.     PF_MAX
  260.     PF_NBS
  261.     PF_NIT
  262.     PF_NS
  263.     PF_OSI
  264.     PF_OSINET
  265.     PF_PUP
  266.     PF_SNA
  267.     PF_UNIX
  268.     PF_UNSPEC
  269.     PF_X25
  270.     SOCK_DGRAM
  271.     SOCK_RAW
  272.     SOCK_RDM
  273.     SOCK_SEQPACKET
  274.     SOCK_STREAM
  275.     SOL_SOCKET
  276.     SOMAXCONN
  277.     SO_ACCEPTCONN
  278.     SO_BROADCAST
  279.     SO_DEBUG
  280.     SO_DONTLINGER
  281.     SO_DONTROUTE
  282.     SO_ERROR
  283.     SO_KEEPALIVE
  284.     SO_LINGER
  285.     SO_OOBINLINE
  286.     SO_RCVBUF
  287.     SO_RCVLOWAT
  288.     SO_RCVTIMEO
  289.     SO_REUSEADDR
  290.     SO_SNDBUF
  291.     SO_SNDLOWAT
  292.     SO_SNDTIMEO
  293.     SO_TYPE
  294.     SO_USELOOPBACK
  295. );
  296.  
  297. if ($^O eq "MacOS") {    
  298.     push @EXPORT, qw(
  299.         AF_PPC
  300.         AF_PAP
  301.         ATALK_SYMADDR
  302.         PF_PPC
  303.         PF_PAP
  304.         ppcNoLocation
  305.         ppcNBPLocation
  306.         ppcNBPTypeLocation
  307.         ppcByCreatorAndType
  308.         ppcByString
  309.     );
  310. }
  311.  
  312. sub sockaddr_in {
  313.     if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
  314.     my($af, $port, @quad) = @_;
  315.     carp "6-ARG sockaddr_in call is deprecated" if $^W;
  316.     pack_sockaddr_in($port, inet_aton(join('.', @quad)));
  317.     } elsif (wantarray) {
  318.     croak "usage:   (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
  319.         unpack_sockaddr_in(@_);
  320.     } else {
  321.     croak "usage:   sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
  322.         pack_sockaddr_in(@_);
  323.     }
  324. }
  325.  
  326. sub sockaddr_un {
  327.     if (wantarray) {
  328.     croak "usage:   (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
  329.         unpack_sockaddr_un(@_);
  330.     } else {
  331.     croak "usage:   sun_sv = sockaddr_un(filename)" unless @_ == 1;
  332.         pack_sockaddr_un(@_);
  333.     }
  334. }
  335.  
  336. sub sockaddr_atlk {
  337.     if (wantarray) {
  338.     croak "usage:   (filename) = sockaddr_atlk(satlk_sv)" unless @_ == 1;
  339.         unpack_sockaddr_atlk(@_);
  340.     } else {
  341.     croak "usage:   satlk_sv = sockaddr_atlk(net,node,socket)" unless @_ == 3;
  342.         pack_sockaddr_atlk(@_);
  343.     }
  344. }
  345.  
  346. sub sockaddr_atlk_sym {
  347.     if (wantarray) {
  348.     croak "Cannot unpack atlk_sym";
  349.     } else {
  350.     croak "usage:   satlk_sym_sv = sockaddr_atlk_sym(obj,type,zone)" unless @_ == 3;
  351.         pack_sockaddr_atlk_sym(@_);
  352.     }
  353. }
  354.  
  355. sub sockaddr_ppc {
  356.     if (wantarray) {
  357.     croak "usage:   (filename) = sockaddr_ppc(sappc_sv)" unless @_ == 1;
  358.         unpack_sockaddr_ppc(@_);
  359.     } else {
  360.     croak "usage:   sppc_sv = sockaddr_ppc(type,name,porttype)" unless @_ == 3;
  361.         pack_sockaddr_ppc(@_);
  362.     }
  363. }
  364.  
  365. sub AUTOLOAD {
  366.     my($constname);
  367.     ($constname = $AUTOLOAD) =~ s/.*:://;
  368.     my $val = constant($constname, @_ ? $_[0] : 0);
  369.     if ($! != 0) {
  370.     if ($! =~ /Invalid/) {
  371.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  372.         goto &AutoLoader::AUTOLOAD;
  373.     }
  374.     else {
  375.         my ($pack,$file,$line) = caller;
  376.         croak "Your vendor has not defined Socket macro $constname, used";
  377.     }
  378.     }
  379.     eval "sub $AUTOLOAD { $val }";
  380.     goto &$AUTOLOAD;
  381. }
  382.  
  383. bootstrap Socket $VERSION;
  384.  
  385. # Preloaded methods go here.  Autoload methods go after __END__, and are
  386. # processed by the autosplit program.
  387.  
  388. 1;
  389. __END__
  390.